package com.loveyukari.controller;

import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.loveyukari.model.Auth;
import com.loveyukari.service.AuthService;
import com.loveyukari.support.JsonResult;

import java.util.List;

/**
*
*yangshaohua
*2017年2月14日下午2:49:15
**/
@Controller
@RequestMapping("/admin/auth")
public class AuthController {
	private Logger log = LoggerFactory.getLogger(AuthController.class);

	@Autowired
	private AuthService authService;

	@RequestMapping("/findIndex")
	public String find(){
		return "auth/find";
	}
	
	@RequestMapping("/detail")
	@ResponseBody
	public JsonResult getAuthById(String name,Integer authId,@RequestParam(defaultValue = "false") boolean followFlag){
		JsonResult<JSONObject> result = new JsonResult<JSONObject>();
		Auth auth = null;
		if (authId != null){
			auth = authService.getCacheAuth(authId);
		} else if (StringUtils.isNotBlank(name)){
			auth = authService.findAuthByName(name);
		}
		if (auth == null){
			return result;
		}
		JSONObject json = new JSONObject();
		json.put("id",auth.getId());
		json.put("name",auth.getName());
		json.put("lv",auth.getLv());
		json.put("fansLv",auth.getFansLv());
		int guizu = auth.getGuizu();
		switch (guizu){
			case 1:json.put("guizu","骑士");break;
			case 2:json.put("guizu","子爵");break;
			case 3:json.put("guizu","伯爵");break;
			case 4:json.put("guizu","公爵");break;
			case 5:json.put("guizu","国王");break;
			case 6:json.put("guizu","皇帝");break;
			case 7:json.put("guizu","游侠");break;
			default:json.put("guizu","无");
		}
		List<String> oldNames = authService.getAuthOldName(auth.getId());
		json.put("oldNames",oldNames);
		List<JSONObject> badges = authService.getAuthBadge(auth.getId());
		json.put("badges",badges);
		List<JSONObject> yubaFollows = authService.getAuthYubaFollow(auth.getId());
		json.put("yubaFollows",yubaFollows);
		if (followFlag){
			List<JSONObject> follows = authService.getAuthFollow(auth.getId());
			json.put("follows",follows);
		}
		result.setData(json);
		return result;
	}


	@RequestMapping("/find")
	@ResponseBody
	public JsonResult<Object> findAuthByName(String name){
		List<Auth> list = authService.findAuthListByName(name);
		if (list.isEmpty()){
			return new JsonResult<Object>(2,"未找到用户");
		}
		return new JsonResult<Object>(0,"",list);
	}

	@RequestMapping("/reloadAll")
	@ResponseBody
	public JsonResult reloadAll(){
		authService.reloadAll();
		return new JsonResult<Object>(0,"");
	}
	
	public String index(){
		return "auth/index";
	}
	
	public String search(){
		return null;
	}
	
}
